package com.dongdongqiang.daogenerator.sqlbuilder.mysql;

import com.dongdongqiang.daogenerator.pojo.TableColumn;
import com.dongdongqiang.daogenerator.pojo.TableMeta;
import com.dongdongqiang.daogenerator.sqlbuilder.InsertSQLBuilder;

import java.util.List;

/**
 * Created by ZZB on 2017/10/19.
 */
public class MysqlInsertSQLBuilder implements InsertSQLBuilder {
    private final static String INSERT_SQL = "insert into {tableName} ({columns}) values ({questionMarks});";

    public String buildInsertSQL(TableMeta tableMeta){
        String tableName = tableMeta.getTableName();
        List<TableColumn> tableColumns = tableMeta.getTableColumns();
        if (tableColumns.size() == 0) return "";
        String columns = tableColumns.stream().map(tableColumn -> "`"+tableColumn.getColumnName()+"`")
                .reduce((s, s2) -> s + "," + s2).get();
        String questionMarks = tableColumns.stream().map(tableColumn -> "?").reduce((s, s2) -> s + "," + s2).get();
        return INSERT_SQL.replace("{tableName}", tableName).replace("{columns}", columns).
                replace("{questionMarks}", questionMarks);
    }

}
